package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.dotos.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {


    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    @Override
    public ResponseResult del(Integer id) {

        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2. 检查频道
        WmChannel channel = getById(id);
        if (channel ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3. 检查频道是否有效
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
        }



        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public PageResponseResult pageList(ChannelDto channelDto) {
        //1.检查参数
        //检查分页
        channelDto.checkParam();

        //2.模糊查询+分页
        IPage page = new Page<>(channelDto.getPage(),channelDto.getSize());

        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(channelDto.getName())){
            lambdaQueryWrapper.like(WmChannel::getName,channelDto.getName());
        }
        page=page(page,lambdaQueryWrapper);

        PageResponseResult result = new PageResponseResult(channelDto.getPage(),channelDto.getSize(), (int) page.getTotal());

        result.setData(page.getRecords());

        return result;
    }

    @Override
    public ResponseResult saveWmChannel(WmChannel wmChannel) {

        //1, 检查参数
        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel channel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if (channel!=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"数据已存在");
        }

        //保存
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(ResponseResult.okResult(AppHttpCodeEnum.SUCCESS));
    }

    @Override
    public ResponseResult updateWnChannel(WmChannel wmChanne) {

        //1. 检查参数
        if (wmChanne ==null && wmChanne.getId() ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        updateById(wmChanne);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}